package keshe;

import keshe.Exception.IdCardException;
import keshe.Exception.PassWordAgainException;
import keshe.Exception.negativeException;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Objects;

public class ACCOUNT implements Serializable {
    @Serial
    private static final long serialVersionUID = 8658993475804416668L;
    private String bankCardId;//银行账户
    private  String userName;//用户名
    private String sex;//性别
    private String passWord;//密码
    private double money;//余额
    private String IDcard;//身份证
    private boolean state=true;//可否使用的状态
    public ArrayList<Info> InfoArrayList=new ArrayList<>();
    public ACCOUNT() {
        bankCardId="00000000";
        userName="张三";
        sex="男";
        passWord="123456";
        money=0;
        IDcard="000000000000000000";
    }
    public ACCOUNT(String bankCardId, String userName, String sex, String passWord, String IDcard) {
        this.bankCardId = bankCardId;
        this.userName = userName;
        this.sex = sex;
        this.passWord = passWord;
        this.IDcard = IDcard;
    }


    //开户方法
//    public void createAccount(String bankCardId){
//        System.out.println("请输入您的储户姓名：");
//        String name= scanner.next();
//        this.userName=name;
//        System.out.println("请输入您的性别：");
//        String sex=scanner.next();
//        this.sex=sex;
//        while (true) {
//            System.out.println("请输入您的身份证号码：");
//            String Idcard=scanner.next();
//            if (Idcard.matches("[1-9](\\d{16})(\\d|X)")) {
//                this.setIDcard(Idcard);
//                break;
//            }else {
//                System.out.println("您输入的身份证号码不符合规则，请重新输入");
//            }
//        }
//        while (true) {
//            System.out.println("请输入您的账户密码：");
//            String pastWord=scanner.next();
//            System.out.println("请确认您的账户密码：");
//            String pastWordagain=scanner.next();
//            if(pastWord.equals(pastWordagain)){
//                break;
//            }else {
//                System.out.println("您两次输入的密码不一致，请重新输入");
//            }
//        }
//        this.passWord=passWord;
//        this.bankCardId=bankCardId;
//        System.out.println("恭喜您！开户成功！");
//        System.out.println("您的银行账户为："+this.bankCardId);
//    }
//开户方法
public void createAccount(String bankId,String name,String sex,String passWord,String passWordAgain,String Idcard) throws IdCardException, PassWordAgainException {
    this.userName=name;
    this.sex=sex;
    if (Idcard.matches("[1-9](\\d{16})(\\d|X)")) {
        this.setIDcard(Idcard);
    }else {
        throw new IdCardException("身份证格式错误！");
    }
    if(passWord.equals(passWordAgain)){
        this.setPassWord(passWord);
    }else {
        throw new PassWordAgainException("两次密码不一致");
    }
    this.bankCardId=bankId;
}



    //存款方法
//    public void depositMoney(){
//        Scanner scanner=new Scanner(System.in);
//        System.out.println("请输入您需要存入的金额：");
//        int money=scanner.nextInt();
//        if(money<0){
//            System.out.println("您不能存入负数的金额！");
//            return;
//        }
//        Info info=new Info();
//        info.setTime();
//        info.setDepositOrDraw(1);
//        info.setMoney(money);
//        InfoArrayList.add(info);
//        this.money+=money;
//        System.out.println("存款成功！");
//        System.out.println("您目前的账户余额为："+this.money);
//    }

    public void depositMoney(double money) throws negativeException {
        if(money<0){
            throw new negativeException("存入金额为负数,存款失败!");
        }
        Info info=new Info();
        info.setTime();
        info.setDepositOrDraw(1);
        info.setMoney(money);
        InfoArrayList.add(info);
        this.money+=money;
    }


    //取款方法
//    public void drawMoney(){
//        Scanner scanner=new Scanner(System.in);
//        System.out.println("请输入您需要取出的金额：");
//        int money=scanner.nextInt();
//        if(money>this.money){
//            System.out.println("您的余额不足，取款失败");
//        }else {
//            Info info=new Info();
//            info.setTime();
//            info.setDepositOrDraw(-1);
//            info.setMoney(money);
//            InfoArrayList.add(info);
//            this.money-=money;
//            System.out.println("取款成功！");
//            System.out.println("您的余额为："+this.money);
//        }
//    }
    public void drawMoney(double money) throws negativeException {
        if(money>this.money){
            throw new negativeException("余额不足，无法取出!");
        }else {
            Info info=new Info();
            info.setTime();
            info.setDepositOrDraw(-1);
            info.setMoney(money);
            InfoArrayList.add(info);
            this.money-=money;
        }
    }


    //查询方法
    public void showAccount(){
        System.out.println(this.userName+"  "+this.sex);
        System.out.println("银行卡号："+this.bankCardId);
        System.out.println("余额："+this.money);
    }

    //查询存取款明细
    public void showInfo(){
        for (int i = 0; i < InfoArrayList.size(); i++) {
            Info info=InfoArrayList.get(i);
            info.soutTime();
            if(info.getDepositOrDraw()==1) System.out.println("  存款"+"  存款金额为："+info.getMoney());
            else System.out.println("  取款"+"  取款金额为："+info.getMoney());
        }
    }


    /**
     * 获取
     * @return bankCardId
     */
    public String getBankCardId() {
        return bankCardId;
    }

    /**
     * 设置
     * @param bankCardId
     */
    public void setBankCardId(String bankCardId) {
        this.bankCardId = bankCardId;
    }

    /**
     * 获取
     * @return userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * 设置
     * @param userName
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * 获取
     * @return sex
     */
    public String getSex() {
        return sex;
    }

    /**
     * 设置
     * @param sex
     */
    public void setSex(String sex) {
        this.sex = sex;
    }

    /**
     * 获取
     * @return passWord
     */
    public String getPassWord() {
        return passWord;
    }

    /**
     * 设置
     * @param passWord
     */
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    /**
     * 获取
     * @return money
     */
    public double getMoney() {
        return money;
    }

    /**
     * 获取
     * @return IDcard
     */
    public String getIDcard() {
        return IDcard;
    }

    /**
     * 设置
     * @param IDcard
     */
    public void setIDcard(String IDcard) {
        this.IDcard = IDcard;
    }

    public String toString() {
        return "ACCOUNT{bankCardId = " + bankCardId + ", userName = " + userName + ", sex = " + sex + ", passWord = " + passWord + ", money = " + money + ",  IDcard = " + IDcard + "}";
    }

    public void setMoney(double money){
        this.money=money;
    }

   public void setState(boolean state){
        this.state=state;
    }
    public boolean getState(){
        return state;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ACCOUNT account = (ACCOUNT) o;
        return  Objects.equals(bankCardId, account.bankCardId) && Objects.equals(userName, account.userName) && Objects.equals(sex, account.sex) && Objects.equals(passWord, account.passWord) ;
    }

    @Override
    public int hashCode() {
        return Objects.hash(bankCardId, userName, sex, passWord, money, IDcard, state, InfoArrayList);
    }

}

